package com.zaofan.controller;

import com.zaofan.bean.Admin;
import com.zaofan.bean.Complaints;
import com.zaofan.service.ComplaintsService;
import com.zaofan.utils.JsonUtils;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/complaints")
public class ComplaintsController extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private ComplaintsService complaintsService = new ComplaintsService();

	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			String selectCityId = "";

			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				if(!name.equals("cityId")){
					params.put(name, value);
				}
				else {

					selectCityId = request.getParameter(name);

				}

			}

			Admin admin = (Admin) request.getSession().getAttribute("admin");
			if(admin.getCityId()!=null&&admin.getCityId()!=0){
				selectCityId = admin.getCityId().toString();
			}
			List<Complaints> complaintss = complaintsService.findAll(admin.getCityId(), params,selectCityId);
			Long count = complaintsService.count(admin.getCityId(), params,selectCityId);
			JsonUtils.writeSuccessPageJson("查询成功", complaintss, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

}
